import User from "./User"
export const USER_LIST_KEY = "user_list_key";

// 根据profile获取用户名
export function getUserNameByProfile(parentNode: HTMLElement): string {
    let nameElement = parentNode.querySelector("[itemprop='http://rdfs.org/sioc/ns#accountName']")

    let name = nameElement?.innerHTML?.trim() + ""

    return name
}

// 获取id
export function getUserIdByProfile(parentNode: HTMLElement): string {
    let idElement = parentNode.querySelector(".author")

    let id = idElement?.id?.replace('user_online_uid_', '')?.trim() + ""

    return id
}

// 获取注册时间
export function getCreatedAtByProfile(parentNode: HTMLElement): string {
    let idElement = parentNode.querySelector("[itemprop='registeredTime']")

    let id = idElement?.innerHTML?.trim() + ""

    return id
}

export function createUser(parentNode: HTMLElement): User {
    let id = getUserIdByProfile(parentNode);
    let username = getUserNameByProfile(parentNode);
    let createdAt = getCreatedAtByProfile(parentNode);
    return new User(username, id, createdAt);
}


function setValue(key: string, value: any): Promise<void> {
    return new Promise<void>(function (resolve) {
        chrome.storage.sync.set({ [key]: value }, function () {
            resolve();
        });
    });
}

function getValue(key: string): Promise<any> {
    return new Promise<any>(function (resolve) {
        chrome.storage.sync.get(key, function (result) {
            resolve(result[key]);
        });
    });
}

function existValue(key: string): Promise<boolean> {
    return new Promise<boolean>(function (resolve) {
        chrome.storage.sync.get(key, function (result) {
            let exist = result[key] ? true : false;
            resolve(exist);
        });
    });
}

export function removeValue(key: string): Promise<void> {
    return new Promise<void>(function (resolve) {
        chrome.storage.sync.remove(key, function () {
            resolve();
        });
    });
}

export async function addBlockedUser(user: User) {
    let exist = await existBlockUser(user.id);

    if (!exist) {
        let userList = await getBlockUsers();
        userList.push(user);
        await setValue(USER_LIST_KEY, userList);
    }
}

export async function getBlockUsers(): Promise<User[]> {
    let userList = await getValue(USER_LIST_KEY);
    if (!userList) {
        userList = new Array();
    }
    return userList;
}

export async function existBlockUser(userId: string): Promise<boolean> {
    let userList = await getBlockUsers();
    let exist = false;

    userList.forEach((element: User) => {
        if (element.id == userId) {
            exist = true;
        }
    });

    return exist;
}

export async function removeBlockUser(userId: string): Promise<void> {
    let userList = await getBlockUsers();
    let newList = new Array<User>();
    userList.forEach((element: User) => {
        if (element.id != userId) {
            newList.push(element);
        }
    });

    await setValue(USER_LIST_KEY, newList);
}